package cn.cuilan.common.codec;

import cn.cuilan.common.entity.HttpEntity;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * 消息编码器
 *
 * @author zhang.yan
 */
@Slf4j
@ChannelHandler.Sharable
public class MessageEncoder extends MessageToByteEncoder<HttpEntity> {

    /**
     * 构造器
     *
     * @param frameLengthFieldLength 消息长度字段所占字节数
     */
    public MessageEncoder(int frameLengthFieldLength) {
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, HttpEntity httpEntity, ByteBuf byteBuf) throws Exception {
        byte[] response = httpEntity.getMessage();
        byteBuf.writeBytes(response);
    }
}
